/*
 * common.h
 *
 *  Created on: 2023年1月3日
 *      Author: ZKSWE Develop Team
 */

#ifndef _COMMON_H_
#define _COMMON_H_

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <set>

#include "os/SystemProperties.h"
#include "utils/Log.h"
#include "manager/ConfigManager.h"
#include "base/fy_utils.h"
#define NORVER   // 关闭宏定义就是寿命测试的


#define BIT(n)                 (1 << (n))
#ifndef BITS_CHECK
#define BITS_CHECK(v, b)       (v & (b))
#endif
#define BITS_SET(v, b)         v |= (b)
#define BITS_CLEAR(v, b)       v &= ~(b)

#define TAB_SIZE(t)            (sizeof(t)/sizeof(t[0]))
#define DELAY(ms)              usleep(ms * 1000)
#define FILE_EXIST(f)          (access(f, F_OK) == 0)
#define CHECK_CP(src, dst)     if (!FILE_EXIST(dst)) { system("cp " src " " dst); }

#define SET_FOREACH(s, t, v)   for (std::set<t>::iterator v = s.begin(); v != s.end(); ++v)

#define RETURN_IF_FAIL(c)             if (!(c)) { return; }
#define RETURN_VALUE_IF_FAIL(c, v)    if (!(c)) { return (v); }

#define TIMER_HOUR(s)          ((s) / 3600)
#define TIMER_MINUTE(s)        ((s) / 60 % 60)
#define TIMER_SECOND(s)        ((s) % 60)
#define TIMER_MK(h, m, s)      ((h) * 3600 + (m) * 60 + (s))
#define RESOURCE(f)            CONFIGMANAGER->getResFilePath(f)
#define WAIT(cond, count, delay)               \
    do {                                       \
        int _count = 0;                        \
        while (!(cond) && (_count < count)) {  \
            _count++;                          \
            DELAY(delay);                      \
        }                                      \
    } while (0)

#define START_SERVICE(name)    SystemProperties::setString("ctl.start", name)
#define STOP_SERVICE(name)     SystemProperties::setString("ctl.stop", name)

#endif /* _COMMON_H_ */
